package bb.lanxing.lib.devices.sprint;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Pair;
import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.Set;

import bb.lanxing.util.Log;

public class LocalSprintNotifier implements SprintNotifier {
    private static final int NOTIFY_CMD_STATUS = 3;
    private static final int NOTIFY_FILE_RECEIVED = 1;
    private static final int NOTIFY_GET_FILELIST = 2;
    private static final int NOTIFY_PROGRESS_UPDATE = 0;
    private static final int NOTIFY_STATUS_CHANGED = 4;
    private static LocalSprintNotifier instance;
    private MainHandler handler = new MainHandler(this);
    private Set<SprintListener> mListeners;

    public static LocalSprintNotifier getInstance() {
        synchronized (LocalSprintNotifier.class) {
            if (instance == null) {
                instance = new LocalSprintNotifier();
            }
        }
        return instance;
    }

    @Override
    public void notifyProgressUpdate(Command command, float f) {
        Message obtainMessage = this.handler.obtainMessage(0, command);
        Bundle bundle = new Bundle();
        bundle.putFloat("progress", f);
        obtainMessage.setData(bundle);
        Log.d("wangsm_bt","LocalSprintNotifier: notifyProgressUpdate");
        this.handler.sendMessage(obtainMessage);
    }

    @Override
    public void notifyFileReceived(String str) {
        Log.d("wangsm_bt","LocalSprintNotifier: notifyFileReceived--> file " + str);
        this.handler.obtainMessage(1, str).sendToTarget();
    }

    @Override
    public void notifyCmdStatus(Command command, int i, byte[] bArr) {
        if(bArr != null){
            Log.d("wangsm_bt","LocalSprintNotifier: notifyCmdStatus--> bArr " + bArr.length);
        }

        this.handler.obtainMessage(3, 0, i, new Pair(command, bArr)).sendToTarget();
    }

    public void registerSprintListener(SprintListener sprintListener) {
        if (this.mListeners == null) {
            this.mListeners = new HashSet();
        }
        Log.d("wangsm_bt","LocalSprintNotifier: registerSprintListener-->  ");
        this.mListeners.add(sprintListener);
    }

    public void unregisterSprintListener(SprintListener sprintListener) {
        Set<SprintListener> set = this.mListeners;
        if (set == null || sprintListener == null) {
            return;
        }
        set.remove(sprintListener);
    }

    public void _notifyProgressUpdate(Command command, float f) {
        Set<SprintListener> set = this.mListeners;
        if (set != null) {
            for (SprintListener sprintListener : set) {
                sprintListener.onProgressUpdate(command, f);
            }
        }
    }

    public void _notifyFileReceived(String str) {
        Set<SprintListener> set = this.mListeners;
        if (set != null) {
            for (SprintListener sprintListener : set) {
                Log.d("wangsm_bt","LocalSprintNotifier: _notifyFileReceived-->  ");
                sprintListener.onFileReceived(str);
            }
        }
    }

    public void _notifyCmdStatus(Command command, int i, byte[] bArr) {
        Set<SprintListener> set = this.mListeners;
        if (set != null) {
            for (SprintListener sprintListener : set) {
                Log.d("wangsm_bt","LocalSprintNotifier: _notifyCmdStatus-->  ");
                sprintListener.onCmdStatus(command, i, bArr);
            }
        }
    }

    private static class MainHandler extends Handler {
        WeakReference<LocalSprintNotifier> notifier;

        public MainHandler(LocalSprintNotifier localSprintNotifier) {
            super(Looper.getMainLooper());
            this.notifier = new WeakReference<>(localSprintNotifier);
        }

        @Override
        public void handleMessage(Message message) {
            LocalSprintNotifier localSprintNotifier = this.notifier.get();
            if (localSprintNotifier == null) {
                return;
            }
            Bundle data = message.getData();
            int i = message.what;
            Log.d("wangsm_bt","LocalSprintNotifier:handleMessage message.what-->  " + i );
            if (i == 0) {
                Log.d("wangsm_bt","LocalSprintNotifier:handleMessage message-->_notifyProgressUpdate  " );
                localSprintNotifier._notifyProgressUpdate((Command) message.obj, data.containsKey("progress") ? data.getFloat("progress") : 0.0f);
            } else if (i == 1) {
                Log.d("wangsm_bt","LocalSprintNotifier:handleMessage message-->_notifyFileReceived  " );
                localSprintNotifier._notifyFileReceived((String) message.obj);
            } else if (i != 3) {
                Log.d("wangsm_bt","LocalSprintNotifier:handleMessage message--> i!=3  " );
            } else {
                Pair pair = (Pair) message.obj;
                Log.d("wangsm_bt","LocalSprintNotifier:handleMessage --> _notifyCmdStatus " );
                localSprintNotifier._notifyCmdStatus((Command) pair.first, message.arg2, (byte[]) pair.second);
            }
        }
    }
}
